package com.fantuan.web;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.q3c.common.util.ObjectUtils;

import com.fantuan.db.bean.Group;
import com.fantuan.db.bean.User;
import com.fantuan.db.bean.UserMoney;

/**
 * 饭团相关
 * 
 * @author Yoyo
 *
 */

@Service
@Controller
public class GroupAction extends BaseAction{

	/**
	 * 饭团首页，饭团信息 + 用户信息 + 用户余额
	 * @param request
	 * @param response
	 * @param groupid
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/groupindex.do")
	public Map<String, Object> processGroupindex(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("groupid") int groupid) throws SQLException
	{
		Map<String, Object> retMap = new HashMap<String, Object>();		
		
		Group group = this.groupDAO.getGroup(groupid);
		
		if( group != null )
		{
			retMap.put("group", group);
			
			List<User> userList = this.userDAO.getUserListByGroupID(groupid);			
			if( !ObjectUtils.isEmptyList(userList) )
			{
				retMap.put("userList", userList);
				List<UserMoney> userMoneyList = new ArrayList<UserMoney>();
				
				for( User user:userList )
				{
					userMoneyList.add(this.userMoneyDAO.getUserMoney(groupid, user.getnID()));
				}
				
				retMap.put("userMoneyList", userMoneyList);
			}
		}
		
		return retMap;
	}
	
	@RequestMapping("/grouplist.do")
	public Map<String, Object> pageGrouplist(
			HttpServletRequest request,
			HttpServletResponse response) throws SQLException
	{
		Map<String, Object> retMap = new HashMap<String, Object>();		
		
		//1.获取登录用户
		User loginUser = (User)request.getSession().getAttribute("login");
		
		//2.排序，按该用户参与消费的时间排序，未参与的不列出
		List<UserMoney> userMoneyList = this.userMoneyDAO.getUserMoneyListByUserID(loginUser.getnID());
				
		//3.获取用户所在的饭团
		List<Group> groupList = new ArrayList<Group>();
		if( !ObjectUtils.isEmptyList(userMoneyList) )
		{
			for( UserMoney userMoney:userMoneyList )
			{
				Group tempGroup = this.groupDAO.getGroup(userMoney.getnGroupID());				
				if( tempGroup != null )
				{
					tempGroup.setDupdateDate(userMoney.getdUpdateDate());//更新成用户最后参与时间
					groupList.add(tempGroup);
				}
			}
		}
		
		//4.获取组成员信息(页面需要人员总数等信息)
		Map<Integer, List<UserMoney>> groupUserMoneyMap = new HashMap<Integer, List<UserMoney>>();
		if( !ObjectUtils.isEmptyList(groupList) )
		{
			for( Group tempgroup:groupList )
			{
				groupUserMoneyMap.put(tempgroup.getnID(), this.userMoneyDAO.getUserMoneyListByGroupID(tempgroup.getnID()) );
			}
		}
		
		//5.加入显示map
		retMap.put("groupList", groupList);
		retMap.put("groupUserMoneyMap", groupUserMoneyMap);
		
		return retMap;
	}
}